package com.fantsey;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author fx
 * @date 2024/7/23
 * @since 1.0
 */
public class PeekDemo {

    public static void main(String[] args) {

        //baseUse();
        //peekForEach();
        //peekLazy();
        //mapLazy();
        peekNotInvoke();
        mapNotInvoke();
        //peekUnModified();
        //mapModified();
    }


    public static void baseUse() {
        List<Integer> list = Stream.of(1,2,3)
                .peek(System.out::println)
                .collect(Collectors.toList());
        System.out.println(list);
    }

    public static void peekForEach() {
        Stream.of(1,2,3)
                .peek(System.out::println)
                .forEach(e ->  System.out.println("forEach:" + e));
    }

    public static void peekLazy() {
        Stream.of(1,2,3)
                .peek(e -> System.out.println("peek lazy: " + e));
    }

    public static void mapLazy() {
        Stream.of(1,2,3)
                .map(e -> {
                    e = e+1;
                    System.out.println("map lazy: " + e);
                    return e;
                });
    }


    public static void peekNotInvoke() {
        long count = Stream.of(1,2,3)
                .peek(e -> System.out.println("peek invoke: " + e))
                .count();
        System.out.println("peekNotInvoke: " + count);

        Stream.of(1,2,3)
                .peek(e -> System.out.println("peek invoke: " + e))
                .collect(Collectors.toList());
        System.out.println("peekInvoke");
    }

    public static void mapNotInvoke() {
        long count = Stream.of(1,2,3)
                .map(e -> {
                    e = e+1;
                    System.out.println("map invoke: " + e);
                    return e;
                }).count();
        System.out.println("mapNotInvoke: " + count);

        Stream.of(1,2,3)
                .map(e -> {
                    e = e+1;
                    System.out.println("map invoke: " + e);
                    return e;
                }).collect(Collectors.toList());
        System.out.println("mapInvoke");
    }


    public static void peekUnModified() {
        Stream.of(1, 2, 3)
                .peek(e -> e=e+1)
                .forEach(e-> System.out.println("peek unModified: "+e));
    }

    public static void mapModified() {
        Stream.of(1, 2, 3)
                .map(e -> e=e+1)
                .forEach(e->System.out.println("map modified: "+e));
    }




}
